<?php
namespace Home\Controller;
use Think\Controller;

class CustomerController extends PublicController {

	protected $user_info;

	public function _initialize() {
		parent::_initialize();

		if (!session('cid')) {
			if (ACTION_NAME == "checkout") {
				session("login_redirect", CONTROLLER_NAME . "/" . ACTION_NAME);
			}
			$this->redirect('Public/login');
		} else {
			$oop = M("customer");
			$this->user_info = $oop->find(session("cid"));
			$this->assign("user_info", $this->user_info);
		}

	}
	/**
	 * 账户首页
	 */
	public function index() {
		$this->display();
	}

	public function profile() {
		$this->display();
	}

	public function updateProfile() {
		try {
			$oop = M("customer");
			$oop->create();
			if (I("new_pwd")) {
				$oop->cs_pwd = md5(I("new_pwd"));
			}
			if ($oop->save()) {
				$this->success("Updated successfully.");
			} else {
				$this->error("Error,please try again.");
			}
		} catch (\Exception $e) {
			if ($e->getCode() == 23000) {
				$this->error("Unqiue error");
			}
		}

	}

	public function order() {
		$oop = D("OrdersView");
		$con['order_cs_id'] = $this->user_info['cs_id'];
		$data = pageInfo($oop, "order_id desc", $con, 25);
		$this->assign("list", $data['list']);
		$this->assign("page", $data['show']);
		$this->display();
	}

	public function orderdetails() {
		$oop = D("OrdersView");
		$con['order_cs_id'] = $this->user_info['cs_id'];
		$con['order_id'] = I("oid");
		$list = $oop->where($con)->find();
		if ($list) {
			$this->assign($list);
			/**
			 *查看订单详情
			 */
			$oop_odd = M("orderdetails");
			$odd_con['odd_order_id'] = I("oid");
			$odd_list = $oop_odd->where($odd_con)->select();
			$this->assign("odd_list", $odd_list);

			/**
			 * 查看订单状态历史
			 */
			$oop_oh = M("orderhistory");
			$oh_con['oh_order_id'] = I("oid");
			$oh_list = $oop_oh->where($oh_con)->select();
			$this->assign("oh_list", $oh_list);

			$this->display();

		} else {
			$this->error("Order not found", U("Customer/order"));
		}
	}

	public function address() {
		$oop = M("address");
		$con['addr_cs_id'] = $this->user_info['cs_id'];
		$con['addr_status'] = 1;
		$data = pageInfo($oop, "addr_id desc", $con, 25);
		$this->assign("list", $data['list']);
		$this->assign("page", $data['show']);
		$this->display();
	}

	public function addressdetails() {
		$oop = M("address");
		$con['addr_cs_id'] = $this->user_info['cs_id'];
		$con['addr_status'] = 1;
		$con['addr_id'] = I("addr_id");
		$list = $oop->where($con)->find();
		if ($list) {
			$this->assign($list);
			$this->display();
		} else {
			$this->error("Address not found", U("Customer/address"));
		}
	}

	public function addAddress() {
		$oop = M("address");
		$oop->create();
		$oop->addr_cs_id = $this->user_info['cs_id'];
		if ($oop->add()) {
			$this->success("Added new address successfully");
		} else {
			$this->error("Error,please try again.");
		}
	}

	public function updateAddress() {
		$oop = M("address");
		$oop->create();
		$oop->addr_cs_id = $this->user_info['cs_id'];
		if ($oop->save()) {
			$this->success("Saved successfully");
		} else {
			$this->error("Error,please try again.");
		}
	}
}